package com.jiutong.meipengdai.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jiutong.meipengdai.R;
import com.jiutong.meipengdai.entity.AddressBean;
import com.jiutong.meipengdai.http.model.interfaces.MyAccountBiz;
import com.jiutong.meipengdai.http.present.MyAccountPresent;
import com.jiutong.meipengdai.ui.activitys.EditAddressActivity;
import com.jiutong.meipengdai.views.dialog.BaseDialog;

import java.util.List;

/**
 * Created by 78 on 2017/6/9.
 */

public class AddAddressAdapter extends RecyclerView.Adapter<AddAddressAdapter.MyViewHolder> implements MyAccountBiz {
    private Context context;
    private List<AddressBean.DataBean.AddresssBean> list;
    private MyAccountPresent present;
    private String type = "del";
    private String id;
    private String name;
    private String provience;
    private String city;
    private String district;
    private String telphone;
    private String detail;
    private String isDefault;
    private int nowPostion;
    BaseDialog dialog;
    private FragmentManager manager;
    private OnChangeListener onChangeListener;
    private String areaCode;

    private void changeColor(TextView view) {
        view.setTextColor(context.getResources().getColor(R.color.orange));
    }


    public AddAddressAdapter(Context context, List<AddressBean.DataBean.AddresssBean> list, FragmentManager manager) {
        this.context = context;
        this.list = list;
        present = new MyAccountPresent(context, this);
        this.manager = manager;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_address, parent, false);
        MyViewHolder viewHolder = new MyViewHolder(view);
        viewHolder.item_tv_address_name = (TextView) view.findViewById(R.id.item_tv_address_name);
        viewHolder.item_iv_address_edit = (ImageView) view.findViewById(R.id.item_iv_address_edit);
        viewHolder.item_tv_address_location = (TextView) view.findViewById(R.id.item_tv_address_location);
        viewHolder.item_tv_address_num = (TextView) view.findViewById(R.id.item_tv_address_num);
        viewHolder.item_tv_address_isdefault = (TextView) view.findViewById(R.id.tv_isdefault);
        viewHolder.item_iv_delete_address = (ImageView) view.findViewById(R.id.iv_delete_address);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {

        getMessage(position);
        boolean editing =  list.get(position).isEdit();
        if (isDefault == "1" || isDefault.equals("1")) {
            holder.item_tv_address_isdefault.setVisibility(View.VISIBLE);
        } else {
            holder.item_tv_address_isdefault.setVisibility(View.GONE);
        }

        holder.item_tv_address_name.setText(name);
        holder.item_tv_address_num.setText("(" + telphone + ")");
        holder.item_tv_address_location.setText(provience + city + district + detail);
        if (editing) {
            holder.item_iv_delete_address.setVisibility(View.VISIBLE);
            holder.item_iv_address_edit.setVisibility(View.VISIBLE);
//            notifyDataSetChanged();
        } else {
            holder.item_iv_delete_address.setVisibility(View.GONE);
            holder.item_iv_address_edit.setVisibility(View.GONE);
//            notifyDataSetChanged();
        }
        holder.item_iv_delete_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog == null) {
                    dialog = new BaseDialog.Builder()
                            .layResuorce(R.layout.dialog_is_delete)
                            .okViewClickEvent(R.id.tv_ok)
                            .cancelViewClickEvent(R.id.tv_cancle)
                            .gravityStyle(Gravity.CENTER)
                            .animResource(R.style.dialog_animation_fade)
                            .dialogWidth(-2)
                            .build();

                }
                getMessage(position);
                dialogClickListener(position);
                dialog.show(manager, "addaddress");

            }
        });

        holder.item_iv_address_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                getMessage(position);
                Intent intent = new Intent(context, EditAddressActivity.class);
                intent.putExtra("id", id);
                intent.putExtra("name", name);
                intent.putExtra("num", telphone);
                intent.putExtra("city", city);
                intent.putExtra("district", district);
                intent.putExtra("provience", provience);
                intent.putExtra("detail", detail);
                intent.putExtra("isDefault", isDefault);
                intent.putExtra("areaCode", areaCode);
                context.startActivity(intent);
            }
        });
    }

    private void getMessage(int position) {
        id = list.get(position).getId();
        name = list.get(position).getRecipients();
        provience = list.get(position).getProvince();
        city = list.get(position).getCity();
        district = list.get(position).getDistrict();
        telphone = list.get(position).getTelphone();
        detail = list.get(position).getDetail();
        isDefault = list.get(position).getPrior();
        areaCode = list.get(position).getAreaCode();
    }

    private void dialogClickListener(final int position) {
        dialog.setOnDialogClickListener(new BaseDialog.OnDialogClickListener() {
            @Override
            public void onOkClick() {
//                if ("1".equals(list.get(position).getPrior())) {
//                    if (list.size() >= 0) {
//                        list.get(0).setPrior("1");
//                    }
//                }
//                nowPostion = position;
                present.saveAddress();
                onChangeListener.onChange("改变了状态");
                dialog.dismiss();
            }

            @Override
            public void onOk2Click() {

            }

        });

    }

    public void setOnChangeListener(AddAddressAdapter.OnChangeListener listener) {
        onChangeListener = listener;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public String getBankNum() {
        return null;
    }

    @Override
    public String getBankName() {
        return null;
    }

    @Override
    public String getCartStatus() {
        return null;
    }

    @Override
    public String getBankId() {
        return null;
    }

    @Override
    public String getMsgId() {
        return null;
    }

    @Override
    public String getLoanId() {
        return null;
    }

    @Override
    public String Consignee() {
        return name;
    }

    @Override
    public String getTelephone() {
        return telphone;
    }

    @Override
    public String getBigLocation() {
        return provience + "-" + city + "-" + district;
    }

    @Override
    public String getDetail() {
        return detail;
    }

    @Override
    public String getIsDefault() {
        return isDefault;
    }

    @Override
    public String getOprType() {
        return type;
    }

    @Override
    public String getMainId() {
        return id;
    }


    @Override
    public void onSuccess(Object object) {
        list.remove(nowPostion);
        notifyDataSetChanged();
    }

    @Override
    public void onError(Throwable e) {

    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        public MyViewHolder(View itemView) {
            super(itemView);
        }

        TextView item_tv_address_num;
        TextView item_tv_address_location;
        TextView item_tv_address_name;
        ImageView item_iv_address_edit;
        TextView item_tv_address_isdefault;
        ImageView item_iv_delete_address;
    }

    public interface OnChangeListener {
        void onChange(String s);
    }
}


